package com.lovi.thread_;

/**
 * 模拟死锁
 */
public class DeadLocal2 {
    public static void main(String[] args) {
        Thread thread = new Thread(new DD(true));
        thread.setName("线程1111");
        thread.start();
        Thread thread1 = new Thread(new DD(false));
        thread1.setName("线程2222");
        thread1.start();

    }
}
class DD implements Runnable{
    static Object o1 = new Object();
    static Object o2 = new Object();
    boolean flag;
    public DD(boolean flag){
        this.flag = flag;
    }

    @Override
    public void run() {
        //在持有一个资源的时候去拿另一个资源
        if(flag){
            synchronized (o1){
                System.out.println(Thread.currentThread().getName() + "进入1");
                synchronized (o2){
                    System.out.println(Thread.currentThread().getName() + "进入2");
                }
            }
        }else {
            synchronized (o2){
                System.out.println(Thread.currentThread().getName() + "进入3");
                synchronized (o1){
                    System.out.println(Thread.currentThread().getName() + "进入4");
                }
            }
        }



    }
}
